//
//  PBMyContractCell.m
//  PBHomeDemo
//
//  Created by Playboy on 2018/3/20.
//  Copyright © 2018年 tmkj. All rights reserved.
//

#import "PBMyContractCell.h"

@interface PBMyContractCell ()

@property (strong, nonatomic) IBOutlet UILabel *tipLabel;
@property (strong, nonatomic) IBOutlet UILabel *nameLabel;
@property (strong, nonatomic) IBOutlet UILabel *idCardLabel;
@property (strong, nonatomic) IBOutlet UILabel *timeLabel;
@property (strong, nonatomic) IBOutlet UILabel *payTypeLabel;
@property (weak, nonatomic) IBOutlet UIButton *applyButton;
@property (nonatomic, strong) PBMyContractModel *model;

@end


@implementation PBMyContractCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)refreshWithModel:(PBMyContractModel *)model {
    self.model = model;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"YYYY/MM/dd";
    // 数字时间戳转化为日期，如果为13位，则需要除以1000，或者去掉后3位
    NSDate *startDate = [NSDate dateWithTimeIntervalSince1970:[[model.startTime substringWithRange:NSMakeRange(0, 10)] doubleValue]];
    NSString *startTime = [formatter stringFromDate:startDate];
    NSDate *endDate = [NSDate dateWithTimeIntervalSince1970:[[model.endTime substringWithRange:NSMakeRange(0, 10)] doubleValue]];
    NSString *endTime = [formatter stringFromDate:endDate];
    self.tipLabel.text = model.orderSnapshot[@"name"];
    self.timeLabel.text = [NSString stringWithFormat:@"%@至%@",startTime,endTime];
    self.payTypeLabel.text = model.orderSnapshot[@"rentPayment"];
    self.nameLabel.text = model.realName;
    self.idCardLabel.text = model.identifyCardNum;
    if (model.applyStatus == 1) {
        [self.applyButton setTitle:@"退租申请中" forState:UIControlStateNormal];
        self.applyButton.userInteractionEnabled = NO;
    }else if (model.applyStatus == 2) {
        [self.applyButton setTitle:@"退租完成" forState:UIControlStateNormal];
        self.applyButton.userInteractionEnabled = NO;
    }else {
        [self.applyButton setTitle:@"退租申请" forState:UIControlStateNormal];
        self.applyButton.userInteractionEnabled = YES;
    }
    
}

// 退租申请
- (IBAction)buttonClick:(UIButton *)sender {
    NSDictionary *param = @{@"contractId":[NSString stringWithFormat:@"%zd",self.model.ID],@"source":@"IOS"};
    [TMNetwork requestPostWithParam:param url:@"/house/return/apply" callback:^(TMBaseReqModel *model) {
        if (model.code == 200) {
            [self.applyButton setTitle:@"退租申请中" forState:UIControlStateNormal];
            self.applyButton.userInteractionEnabled = NO;
        }else {
            [self showHint:model.message];
        }
    }];
}

@end
